package com.ruoyi.project.blog.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.project.blog.domain.vo.ApiFriendLinkVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.blog.domain.BFriendLink;
import com.ruoyi.project.blog.service.IBFriendLinkService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 友情链接Controller
 * 
 * @author ruoyi
 * @date 2023-10-09
 */
@RestController
@RequestMapping("/webManage/friendLink")
public class BFriendLinkController extends BaseController
{
    @Autowired
    private IBFriendLinkService bFriendLinkService;

    /**
     * 查询友情链接列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BFriendLink bFriendLink)
    {
        //判断是否是管理员，管理员查看所有数据，否则值查看自己的数据
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        if(!admin){
            bFriendLink.setUserId(SecurityUtils.getUserId().toString());
        }
        startPage();
        List<BFriendLink> list = bFriendLinkService.selectBFriendLinkList(bFriendLink);
        return getDataTable(list);
    }

    /**
     * 导出友情链接列表
     */
    @Log(title = "友情链接", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BFriendLink bFriendLink)
    {
        //判断是否是管理员，管理员查看所有数据，否则值查看自己的数据
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        if(!admin){
            bFriendLink.setUserId(SecurityUtils.getUserId().toString());
        }
        List<BFriendLink> list = bFriendLinkService.selectBFriendLinkList(bFriendLink);
        ExcelUtil<BFriendLink> util = new ExcelUtil<BFriendLink>(BFriendLink.class);
        util.exportExcel(response, list, "友情链接数据");
    }

    /**
     * 获取友情链接详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bFriendLinkService.selectBFriendLinkById(id));
    }

    /**
     * 新增友情链接
     */

    @Log(title = "友情链接", businessType = BusinessType.INSERT)
    @PostMapping
    @Anonymous
    public AjaxResult add(@RequestBody BFriendLink bFriendLink)
    {
        String userId = "";
        try {
            userId   = SecurityUtils.getUserId().toString();
        } catch (Exception e) {
            userId = "网章未登录";
        }
        bFriendLink.setUserId(userId);
        return toAjax(bFriendLinkService.insertBFriendLink(bFriendLink));
    }

    /**
     * 修改友情链接
     */

    @Log(title = "友情链接", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BFriendLink bFriendLink)
    {
        return toAjax(bFriendLinkService.updateBFriendLink(bFriendLink));
    }

    /**
     * 删除友情链接
     */
    @Log(title = "友情链接", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bFriendLinkService.deleteBFriendLinkByIds(ids));
    }

    /**
     * 网站上友情链接列表，只显示上架的友情链接
     * @param
     * @return
     */
    @GetMapping("/listForWeb")
    @Anonymous
    public AjaxResult listForWeb()
    {
        List<ApiFriendLinkVO> list = bFriendLinkService.selectLinkList();
        return AjaxResult.success(list);
    }
}
